<?php 

namespace app\admin\event;

use app\common\model\BaseGoods as BaseGoodsModel;
use app\common\utils\JsonService;

class BaseGoodsEvent{

    /*
    * 列表
    */
    public static function baseGoodsList(){
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $where = [];
        if($id = isset($params['id'])){
                    $where[] = [['id','=',$params['id']]];
            }
            if($product_name = isset($params['product_name'])){
                    $where[] = [['product_name','like',"%{$params['product_name']}%"]];
            }
            if($cat_id = isset($params['cat_id'])){
                    $where[] = [['cat_id','like',"%{$params['cat_id']}%"]];
            }
            if($figure_nu = isset($params['figure_nu'])){
                    $where[] = [['figure_nu','like',"%{$params['figure_nu']}%"]];
            }
             $condition = [
            'where' => $where,
            'order' => ['id' => 'desc'],
                'paginate' => [
                'page' => $page,
                'query' => $params,
            ],
        ];
        $baseGoods = BaseGoodsModel::getPageAll($condition);
        return $baseGoods;
    }


    /*
    * 新增
    */
    public static function createBaseGoods($data){

        try {
            BaseGoodsModel::create($data);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }

    /*
    * 更新
    */
    public static function updateBaseGoods($data){

        try {
            BaseGoodsModel::updateOne($data, ['id' => $data['id']]);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }


}

